package com.dkts.oil.test.road;



import com.dkts.oil.util.gis.DecimalFormatUtils;
import org.locationtech.jts.geom.*;
import org.locationtech.jts.operation.overlay.snap.GeometrySnapper;

import java.util.ArrayList;
import java.util.List;

public class ConflictCheck {

    public static List<Coordinate> check(LineString intersectionLine, Geometry polygon) {
        Geometry geometryd = polygon.intersection(intersectionLine);
        if (geometryd.getCoordinates().length == 0) {
            return new ArrayList<>();
        }
        Coordinate coordinate = geometryd.getCoordinates()[0];
        Coordinate coordinate2 = geometryd.getCoordinates()[geometryd.getCoordinates().length - 1];

        String key1 = DecimalFormatUtils.getDecimalFormatUtils(coordinate.x) + "-" + DecimalFormatUtils.getDecimalFormatUtils(coordinate.y);
        String key2 = DecimalFormatUtils.getDecimalFormatUtils(coordinate2.x) + "-" + DecimalFormatUtils.getDecimalFormatUtils(coordinate2.y);
//        System.out.println(coordinate.x+" "+coordinate.y);
//        System.out.println(coordinate2.x+" "+coordinate2.y);
        // 获取面的边界
        Geometry polygonBoundary = polygon.getBoundary();

        // 将交叉线段与面的边界进行交集操作，以添加交叉点
        // 更新面
        GeometryCollection updatedPolygon;
        Geometry updatedBoundary;
        try {
            updatedBoundary = polygonBoundary.union(intersectionLine);
            Geometry union = polygon.difference(polygonBoundary);
            updatedPolygon = (GeometryCollection) union.union(updatedBoundary);
        } catch (Exception e) {
            System.out.println("跟新错误" + intersectionLine);
            e.printStackTrace();
            return null;
        }


        int row = updatedPolygon.getNumGeometries();
        for (int i = 0; i < row; i++) {
            Geometry geometry = updatedPolygon.getGeometryN(i);
            if (geometry instanceof Polygon) {
                int startIndex = 0;
                int endIndex = 0;
                Coordinate[] coordinates = geometry.getCoordinates();
                for (int j = 0; j < coordinates.length; j++) {
                    Coordinate coordinate1 = coordinates[j];
                    String temp = DecimalFormatUtils.getDecimalFormatUtils(coordinate1.x) + "-" + DecimalFormatUtils.getDecimalFormatUtils(coordinate1.y);
                    if (key1.equals(temp)) {
                        startIndex = j;
                    }
                    if (key2.equals(temp)) {
                        endIndex = j;
                    }
                }
                List<Coordinate> jsonArray = new ArrayList<>(endIndex);
                if (startIndex > endIndex) {

                    for (int z = startIndex; z < coordinates.length; z++) {
                        Coordinate coordinate1 = coordinates[z];
                        jsonArray.add(coordinate1);
                    }
                    for (int j = 0; j < endIndex; j++) {
                        Coordinate coordinate1 = coordinates[j];
                        jsonArray.add(coordinate1);
                    }
                } else {
                    for (int h = startIndex; h <= endIndex; h++) {
                        Coordinate coordinate1 = coordinates[h];
                        jsonArray.add(coordinate1);
                    }

                }
                return jsonArray;
            }
        }
        return new ArrayList<>();
    }

    // 使用线分割多边形
    private static Geometry[] splitPolygonByLine(Polygon polygon, LineString line) {
        GeometryFactory geometryFactory = new GeometryFactory();

        // 使用GeometrySnapper修正多边形和线的交点
        Geometry[] snappedLine = GeometrySnapper.snap(line, polygon, 0.001);
        for (int i = 0; i < snappedLine.length; i++) {
//            System.out.println(GeoSubStringUtils.lonlatTemp(snappedLine[i].toText()));
        }

        // 使用线分割多边形
//        Geometry nodedLinework = polygon.getBoundary().union(snappedLine[0]);

        // 构造多边形
//        Polygonizer polygonizer = new Polygonizer();
//        polygonizer.add(nodedLinework);
//
//        @SuppressWarnings("unchecked")
//        Collection<Polygon> polygons = polygonizer.getPolygons();
//        polygons.forEach(a->{
//            System.out.println(GeoSubStringUtils.lonlatTemp(a.toText()));
//        });
//
//        if (polygons.size() == 2) {
//            return polygons.toArray(new Polygon[0]);
//        }

        return null;
    }
}
